﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ASoft.Web.Base;

namespace Asoft.Web.Controllers
{
    public class AUserController : EntityController<AUserRepository, AUser>
    {
        protected override ASoft.Base.PageResult<AUser> GetIndex1PageResult()
        {
            var result = Repository.GetPageResult(null, PageIndex, PageSize, new string[] { "code" }, new string[] { "asc" });
            return result;
        }

        protected override void ProcessJQGridEdit(AUser entity)
        {
            entity.Code = Request["Code"];
            entity.CName = Request["CName"];
            entity.Enable = 1;
            entity.CPassword = Request["CPassword"];
        }

        [HttpPost]
        public ActionResult Delete()
        {
            var id = int.Parse(Request["AUserId"]);
            Repository.Delete(id);
            return GetJsonResult("success", "删除成功");
        }


        [HttpPost]
        public ActionResult Update()
        {
            var id = int.Parse(Request["AUserId"]);
            var entity = Repository.GetById(id);
            if (!string.IsNullOrWhiteSpace(Request["CPassword"]))
            {
                entity.CPassword = Request["CPassword"];
            }
            return ProcessEntity(entity, Repository.Update, "更新成功");
        }

        [HttpPost]
        public ActionResult Add()
        {
            var entity = new AUser();
            entity.CPassword = Request["CPassword"];
            if (string.IsNullOrWhiteSpace(entity.CPassword))
            {
                return GetJsonResult("error", "密码不能为空");
            }
            return ProcessEntity(entity, Repository.Insert, "添加成功");
        }

        public ActionResult ProcessEntity(AUser entity, Action<AUser> action, string msg)
        {

            var status = "error";

            entity.Code = Request["Code"];
            entity.CName = Request["CName"];
            entity.Enable = 1;


            if (string.IsNullOrWhiteSpace(entity.Code))
            {
                return GetJsonResult(status, "编码不能为空");
            }

            if (string.IsNullOrWhiteSpace(entity.CName))
            {
                return GetJsonResult(status, "名称不能为空");
            }

            entity.Code = entity.Code.Trim();
            entity.CName = entity.CName.Trim();
            entity.CPassword = entity.CPassword.Trim();

            action(entity);

            status = "success";
            return GetJsonResult(status, msg);

        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(string oldPassword, string newPassword1, string newPassword2)
        {
            oldPassword = oldPassword.Trim();
            newPassword1 = newPassword1.Trim();
            newPassword2 = newPassword2.Trim();

            var status = "error";
            var msg = "error";
            if (newPassword1.Length == 0 || newPassword2.Length == 0)
            {
                msg = "新密码长度必须大于0,请重新输入.";
                return GetJsonResult(status, msg);
            }
            if (!string.Equals(newPassword1, newPassword2))
            {
                msg = "两次新密码不一致,请重新输入.";
                return GetJsonResult(status, msg);
            }
            if (!string.Equals(CurrentContext.AUser.CPassword, oldPassword))
            {
                msg = "原密码输入错误,请重新输入.";
                return GetJsonResult(status, msg);
            }
            var auserRep = new AUserRepository();
            var auser = auserRep.GetById(CurrentContext.AUser.AUserId);
            auser.CPassword = newPassword1;
            auserRep.Update(auser);
            CurrentContext.AUser = auser;
            status = "success";
            msg = "修改成功";
            return GetJsonResult(status, msg);
        }
       
    }
}
